package com.example.lewisgao.blankactivity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by LewisGao on 2015-06-26 0026.
 */
public class ProgressBarActivity extends Activity implements View.OnClickListener {
    Button buttonAdd;
    Button buttonReduce;
    Button buttonReset;
    TextView textView;
    ProgressBar progressBar4;
    ProgressDialog progressDialog;
    Button buttonShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.progress);

        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        setProgress(600);

        progressBar4 = (ProgressBar) findViewById(R.id.progressBar4);
        buttonAdd = (Button) findViewById(R.id.buttonAdd);
        buttonReduce = (Button) findViewById(R.id.buttonReduce);
        buttonReset = (Button) findViewById(R.id.buttonReset);
        textView = (TextView) findViewById(R.id.textView);
        buttonAdd.setOnClickListener(this);
        buttonReduce.setOnClickListener(this);
        buttonReset.setOnClickListener(this);

        buttonShow = (Button) findViewById(R.id.show);
        buttonShow.setOnClickListener(this);

        init();

    }

    private void init() {
        int first = progressBar4.getProgress();
        int second = progressBar4.getSecondaryProgress();
        int max = progressBar4.getMax();
        textView.setText("第一进度百分比：" + (int) (first / (float) max * 100) + "% 第二进度百分比：" + (int) (second / (float) max * 100) + "%");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonAdd: {
                progressBar4.incrementProgressBy(10);
                progressBar4.incrementSecondaryProgressBy(10);
                break;
            }
            case R.id.buttonReduce: {
                progressBar4.incrementProgressBy(-10);
                progressBar4.incrementSecondaryProgressBy(-10);
                break;
            }
            case R.id.buttonReset: {
                progressBar4.setProgress(50);
                progressBar4.setSecondaryProgress(80);
                break;
            }
            case R.id.show: {
                progressDialog = new ProgressDialog(ProgressBarActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("hello world!");
                progressDialog.setMessage("welcome come to Android");
                progressDialog.setIcon(R.drawable.ic_launcher);

                progressDialog.setMax(100);
                progressDialog.incrementProgressBy(50);
                progressDialog.setIndeterminate(false);

                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(ProgressBarActivity.this, "welcome", Toast.LENGTH_SHORT);
                    }
                });

                progressDialog.setCancelable(true);
                progressDialog.show();
                break;
            }
        }
        init();
    }
}
